<template>
	<view class="content">
		<image class="logo" src="../../static/logo.jpg" mode=""></image>
		<view class="text-area" @click="login">
			<text class="title">{{title}}</text>
		</view>

		<!-- 弹框 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000">
			</uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import store from '../../store/index.js'; //仓库有问题
	import userApi from '../../api/user.js'; //请求接口
	export default {
		data() {
			return {
				title: '登陆',
				msgType: "",
				messageText: ""
			}
		},
		methods: {
			//点击登陆按钮
			login(e) {
				let _this = this
				// 获取用户信息
				uni.getUserProfile({
					desc: "login",
					lang: "zh_CN",
					success: ({
						userInfo
					}) => {
						const {
							avatarUrl,
							nickName
						} = userInfo;
						uni.login({
							provider: "weixin",
							scopes: "auth_base",
							success: ({
								code
							}) => {
								userApi.login({
									nickName,
									img: avatarUrl,
									code,
									cb: (data) => {
										if (data) {
											//存放到仓库
											this.$store.commit('user/set_token', data
												.token)
											this.$store.commit('user/set_img', data
												.img)
											this.$store.commit('user/set_nickName',
												data.nickName)
											this.$store.commit('user/set_id', data
											._id);
											this.$store.commit('user/set_delivery', data
											.delivery);
											
											this.msgType = "success";
											this.messageText =
												` 用户： ${data.nickName} 登陆成功 `;
											this.$refs.message.open()
											setTimeout(
												() => {
													uni.switchTab({
														url: '/pages/home/home',
													})
												}
											, 1000)
										} else {
											//错误
										}
									}
								});
								// this.$store.dispatch('user/login', {
								// 	nickName,
								// 	img: avatarUrl,
								// 	code,
								// 	success: (data) => {
								// 		console.log(data)
								// 	}
								// })
							}
						})

					},
					fail: () => {
						this.msgType = "error";
						this.messageText = '用户拒绝登陆';
						this.$refs.message.open()
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin: 300rpx auto 250rpx;
	}

	.text-area {
		width: 400rpx;
		border-radius: 40rpx;
		height: 80rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #78cdd1;
	}

	.title {
		font-size: 36rpx;
		color: white;
	}
</style>
